package com.itheima.comtroller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetmealService setmealService;
    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> setmeals;
        try {
            setmeals= setmealService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmeals);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
    @RequestMapping("/findById")
    public Result findById(int id){
       Setmeal setmeals;
        try {
            setmeals= setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmeals);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
    @RequestMapping("/code")  //发送短信
    public Result findById(String phone) {
        try {
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phone,code.toString());
            //将生成的验证码存到redis中,设置保存时间为3分钟
            jedisPool.getResource().setex(RedisMessageConstant.SENDTYPE_ORDER+phone,5*60,code.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
    @RequestMapping("/saveOrder")
    public Result saveOrder(@RequestBody Map<String,Object> map) {
        System.out.println(map);

        String telePhone = (String) map.get("telephone");
        String s = jedisPool.getResource().get(RedisMessageConstant.SENDTYPE_ORDER + telePhone);
        System.out.println(s);
        //判断验证码是否一致
        if( map.get("validateCode").equals(s)){
            Result save = null;
            try {
                save = setmealService.save(map);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return save;
        }else{
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

    }

}
